package vh.sphynx.runtime.impl;

import vh.sphynx.runtime.VariableStorage;
import vh.sphynx.util.CharSequenceStream;

public class VariableImpl implements VariableStorage {

	private int type;
	private boolean global;
	private Object value;
	
	public VariableImpl(int type, boolean global) {
		this.global = global;
		this.type = type;
		value = EMPTY;
	}

	@Override
	public Object getValue() {
		return value;
	}

	@Override
	public Object setValue(Object value) {
		return (this.value = value);
	}

	@Override
	public boolean isWritable() {
		return type != READONLY;
	}

	@Override
	public void setWritable(boolean writable) {
		if (writable)
			type = VARIABLE;
		else
			type = READONLY;
	}
	
	@Override
	public boolean isGlobal() {
		return global;
	}

	@Override
	public int getVariableType() {
		return type;
	}

	@Override
	public String toString() {
		if (value instanceof CharSequenceStream)
			return ((CharSequenceStream) value).toString(false);
		return value != null ? value.toString() : "null";
	}

	@Override
	public boolean isEmpty() {
		return value == EMPTY;
	}
}
